package activity;

import java.util.ArrayList;
import java.util.List;

import persistencia.DALOS;
import persistencia.WebS;
import persistencia.WebServices;

import modelo.Cliente;
import modelo.OS;

import teste.exemplo.ws.AdapterListView;
import teste.exemplo.ws.R;
import teste.exemplo.ws.R.id;
import teste.exemplo.ws.R.layout;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class OsListViewActivity extends Activity implements
		View.OnClickListener, OnItemClickListener {
	/** Called when the activity is first created. */
	
	private ListView listView;
    private AdapterListView adapterListView;
    private ArrayList<OS> lstOSs;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.os);

//		Button b = (Button) findViewById(R.id.btnId);
//		b.setOnClickListener(this);

		listView = (ListView) findViewById(R.id.listViewLista);
        listView.setOnItemClickListener(this);

        createListView();
	}

	public void onClick(View v) {
		try {

			// WebS s = new WebS();
			// List<Cliente> teste = s._teste();
			// int isso = teste.size();
			// Toast.makeText(getApplicationContext(), Integer.toString(isso),
			// Toast.LENGTH_LONG).show();

			// Toast.makeText(getApplicationContext(),
			// String.valueOf(s._teste()), Toast.LENGTH_SHORT).show();
			// createListView();
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), e.getMessage(),
					Toast.LENGTH_SHORT).show();

			// this._setTexto(e.getMessage());
		}
	}

	private void createListView() {
		
		//WebS s = new WebS();
		try {
			
			
			DALOS oOSs = new DALOS();
			
			List<OS> os = oOSs.ListarOS();
		
		
			lstOSs = new ArrayList<OS>();
		
		for (OS ss : os) {
			//Cliente item1 = new Cliente(oCliente.getIdCliente(), oCliente.getNome(), oCliente.getEndereco());
			lstOSs.add(ss);
		}

        adapterListView = new AdapterListView(this, lstOSs);

        listView.setAdapter(adapterListView);
        } catch (Exception e) {
        	Toast.makeText(getApplicationContext(), e.getMessage(),
					Toast.LENGTH_SHORT).show();
		}
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    	OS oOs = adapterListView.getItem(arg2);
//        Toast.makeText(this, "Os selecionada: " + oOs.getCod(), Toast.LENGTH_SHORT).show();
        
//        Cliente cliente = new Cliente();
//		cliente.setIdCliente(oOs.getoCliente().getIdCliente());
//		cliente.setNome(oOs.getoCliente().getNome());
//		cliente.setEndereco(oOs.getoCliente().getEndereco());
//		cliente.setLatitude(oOs.getoCliente().getLatitude());
//		cliente.setLongitude(oOs.getoCliente().getLongitude());
		
		Intent it = new Intent(getBaseContext(), ClienteActivityTeste.class);
		
		it.putExtra("cliente", oOs.getoCliente());

		startActivity(it);
    }

}